package com.open.capacity.form.service.impl;


import com.open.capacity.commons.Result;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import com.open.capacity.form.dao.FormDbLinkDao;
import com.open.capacity.form.model.FormDbLink;
import com.open.capacity.form.service.FormDbLinkService;
import com.open.capacity.form.vo.FormDbLinkVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class FormDbLinkServiceImpl implements FormDbLinkService {

    @Autowired
    private FormDbLinkDao dbLinkDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(FormDbLink dbLink) {
        dbLink.setCreateTime(new Date());
        dbLink.setUpdateTime(dbLink.getCreateTime());

        dbLinkDao.save(dbLink);
        log.info("保存数据库连接：{}", dbLink);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(FormDbLink dbLink) {
        dbLink.setUpdateTime(new Date());

        dbLinkDao.updateByOps(dbLink);
        log.info("修改数据库连接：{}", dbLink);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        FormDbLink dbLink = dbLinkDao.findById(id);

        dbLinkDao.delete(id);

        log.info("删除数据库连接：{}", dbLink);
    }


    @Override
    public FormDbLink findById(String id) {
        return dbLinkDao.findById(id);
    }

    @Override
    public List<FormDbLink> findDbLinks(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<FormDbLink> list = dbLinkDao.findList(params);
        return list;
    }

    @Override
    public Map<String, FormDbLinkVo> getMap() {
        Map<String, FormDbLinkVo> map = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        List<FormDbLink> list = dbLinkDao.findList(params);
        FormDbLinkVo vo;
        for (FormDbLink item : list) {
            vo = new FormDbLinkVo();
            vo.setAlias(item.getDbAlias());
            vo.setName(item.getDbName());
            map.put(item.getId(), vo);
        }
        return map;
    }


    @Override
    public Result saveOrUpdate(FormDbLink dbLink, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            FormDbLink model = dbLinkDao.findById(keyValue);
            if (model != null) {
                dbLink.setId(keyValue);
                dbLink.setUpdateTime(new Date());
                i = dbLinkDao.updateByOps(dbLink);
            }
        } else {
            dbLink.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            dbLink.setCreateTime(new Date());
            dbLink.setUpdateTime(dbLink.getCreateTime());
            i = dbLinkDao.save(dbLink);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
